import { setAuthorization } from '~/composables/authorization'

export interface LoginParams {
  userPhone: string
  userPassword?: string
  verificationcode?: string
  captcha_id?: string
  captcha_text?: string
}

export interface LoginMobileParams {
  userPhone: string
  userPassword: string
  verificationcode: string
}

export interface LoginResultModel {
  success: boolean
  message: string
  userName: string | null
  userCompany: string | null
  userPhone: string | null
  token: string | null
}

// 发送短信验证码
export interface SendSmsParams {
  phone: string
  ttl_seconds: number
  captcha_id?: string
  captcha_text?: string
}

export interface SendSmsResult {
  phone: string
  ttl_seconds: number
}

export function sendSmsCodeApi(params: SendSmsParams) {
  return usePost<SendSmsResult, SendSmsParams>('/api/v1/auth/sms/send', params, {
    token: false,
    customDev: true,
    loading: false, // 关闭全屏加载蒙版
  })
}

// 手机号 + 验证码 登录
export interface PhoneLoginParams {
  userPhone: string
  userPassword: string
  verificationcode: string
}

export function phoneLoginApi(params: PhoneLoginParams) {
  return usePost<LoginResultModel, PhoneLoginParams>('/api/v1/users/login', params, {
    token: false,
    customDev: true,
    loading: true,
  })
}

export function loginApi(params: LoginParams) {
  return (usePost<any, LoginParams>('/api/v1/users/login', params, {
    token: false,
    customDev: true,
    loading: true,
  }) as unknown as Promise<any>).then((res) => {
    // 期望后端返回：{ code: 0, message: 'OK', data: { token, userName, ... } }
    try {
      if (res && typeof res === 'object' && res.code === 0) {
        const token = res?.data?.token ?? null
        if (token)
          setAuthorization(token)
      }
    }
    catch {}
    return res as any
  })
}

//   提交报名 接口
export interface SubmitRegistrationParams {
  [key: string]: any
}

export interface SubmitRegistrationResult {
  code: number
  message?: string
  data?: any
}
export function submitRegistrationApi(params: SubmitRegistrationParams) {
  return usePost<SubmitRegistrationResult, SubmitRegistrationParams>(
    '/api/v1/users/registration',
    params,
    {
      token: true, // 自动从 useAuthorization() 获取 token
      customDev: true,
      loading: true,
    },
  ) as unknown as Promise<SubmitRegistrationResult>
}

export interface updateRegistrationParams {
  [key: string]: any
}

export interface updateRegistrationResult {
  code: number
  message?: string
  data?: any
}

export function updateRegistrationApi(params: updateRegistrationParams) {
  return usePut<SubmitRegistrationResult, updateRegistrationParams>(
    `/api/v1/users/registrations/${params.id}`,
    params,
    {
      token: true, // 自动从 useAuthorization() 获取 token
      customDev: true,
      loading: true,
    },
  ) as unknown as Promise<updateRegistrationResult>
}

// 注册
export interface RegisterParams {
  userName: string
  userCompany: string
  userPassword: string
  confirmPassword: string
  userPhone: string
  verificationcode: string
}

export interface RegisterResult {
  success: boolean
  message: string
  userName: string | null
  userCompany: string | null
  userPhone: string | null
}

export function registerApi(params: RegisterParams) {
  return usePost<RegisterResult, RegisterParams>('/api/v1/users/register', params, {
    token: false,
    customDev: true,
    loading: true,
  }) as unknown as Promise<RegisterResult>
}

// 图形验证码接口
export interface CaptchaParams {
  captcha_id?: string
}

export interface CaptchaResult {
  code: number
  message: string
  data: {
    captcha_id: string
    image_base64: string
  }
}

export function getCaptchaImageApi(params?: CaptchaParams) {
  return useGet<CaptchaResult>('/api/v1/auth/captcha/image', params || {}, {
    token: false,
    customDev: true,
    loading: false,
  })
}

// 我的报名列表
export interface RegistrationItem {
  id: number
  subject_code: string
  description: string
  exam_subject: object
  registration_status: number
  exam_fee: string
  paid_amount: string
  registration_time: string
  makeup_unit: string
  pre_exam_training: number
  username: string
  phone: string
  email: string
}

// 定义subeject 字段输出
export interface GetSubjectResult {
  code: number
  message?: string
  data?: any
}

export interface RegistrationListResult {
  code: number
  message: string
  data: RegistrationItem[]
}

export interface RegistrationDetailResult {
  code: number
  message: string
  data: RegistrationItem[]
}

export function getRegistrationsApi() {
  return useGet<RegistrationListResult>('/api/v1/users/registrations', {}, {
    token: true,
    customDev: true,
    loading: true,
  })
}

// GET方法 获取 用户某次参加考试的报名详情
//  参数 registration_id 考试报名项目的ID
export function getRegistrationDetailApi(registration_id: string) {
  return useGet<RegistrationDetailResult>(`/api/v1/users/registrations/${registration_id}`, {}, {
    token: true,
    customDev: true,
    loading: true,
  })
}
// 获取考试科目列表
export function getSubjectApi() {
  return useGet<GetSubjectResult>('/api/v1/users/subjects', {}, {
    token: true,
    customDev: true,
    loading: true,
  })
}
// CSDG_FIRST suject_code
// 获取单个倒是科目的详细信息
export function getSubjectDetailApi(subject_code: string) {
  return useGet<GetSubjectResult>(`/api/v1/users/subjects?subject_code=${subject_code}`, {}, {
    token: true,
    customDev: true,
    loading: true,
  })
}
